# -*- coding:utf-8 -*- 

import os
import urllib.request 
import sys, getopt
import time

#Download FinancialData and FinancialData_pro from the given website into specified paths
#-a AllFinancialDataPath -f FinancialDataPath

def main(argv):
    output_AllFD = ""
    output_FD = ""

    try:
        opts, args = getopt.getopt(argv, "a:f:",["output_AllFD=", "output_FD="])
    except getopt.GetoptError:
        print ('Error: test_arg.py -a <output_AllFD> -f <output_FD>')
        sys.exit(2)

    for opt, arg in opts:
        if opt == "-h":
            print ('test_arg.py -a <output_AllFD> -f <output_FD>')
            sys.exit()
        elif opt in ("-a", "--output_AllFD"):
            output_AllFD = arg
        elif opt in ("-f", "--output_FD ="):
            output_FD = arg

    response = urllib.request.urlopen("http://www.yucezhe.com/api/download/product/latest/update?token=d67f23b819772&pdt_name=financial-data")    
    strWebA = response.read()
    strWebA = strWebA.decode('UTF-8')
    rs = urllib.request.urlopen(strWebA).read()
    path = os.path.join(output_FD,time.strftime("%Y%m%d", time.localtime()))
    if not os.path.exists(path):
        os.mkdir(path)
    open(os.path.join(path,'financial_data.zip'), 'wb').write(rs)

    response = urllib.request.urlopen("http://www.yucezhe.com/api/download/product/latest/update?token=0d50cea519772&pdt_name=financial-data-pro")
    strWebB = response.read()
    strWebB = strWebB.decode('UTF-8')
    rs = urllib.request.urlopen(strWebB).read()
    path = os.path.join(output_AllFD,time.strftime("%Y%m%d", time.localtime()))   
    if not os.path.exists(path):
        os.mkdir(path)
    open(os.path.join(path,'all_financial_data.zip'), 'wb').write(rs)

    print ('---Finsih---')

if __name__ == "__main__":
    main(sys.argv[1:])